{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "K8S_PROXY_ADDR='127.0.0.1:8001'\n",
    "K8S_NAMESPACE='mdt'\n",
    "APP_NAME='sklearn-classification'\n",
    "MODEL_NAME='clipper-sklearn-predict'\n",
    "MODEL_FILE='classification.pkl'\n",
    "REPO_URL='658391232643.dkr.ecr.us-west-2.amazonaws.com'\n",
    "VERSION = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Train the Model and persist it into a pickle file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.13991367 0.01410133 0.33966798 0.50631702]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "      setosa       1.00      1.00      1.00        19\n",
      "  versicolor       0.94      1.00      0.97        15\n",
      "   virginica       1.00      0.94      0.97        16\n",
      "\n",
      "   micro avg       0.98      0.98      0.98        50\n",
      "   macro avg       0.98      0.98      0.98        50\n",
      "weighted avg       0.98      0.98      0.98        50\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/gtao/Library/Python/2.7/lib/python/site-packages/sklearn/ensemble/forest.py:248: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.\n",
      "  \"10 in version 0.20 to 100 in 0.22.\", FutureWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['classification.pkl']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.externals import joblib\n",
    "\n",
    "data = load_iris()\n",
    "\n",
    "X, y = data[\"data\"], data[\"target\"]\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.33, random_state=42)\n",
    "\n",
    "clf = RandomForestClassifier(max_depth=2, random_state=0)\n",
    "clf.fit(X_train, y_train)\n",
    "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
    "                       max_depth=2, max_features='auto', max_leaf_nodes=None,\n",
    "                       min_impurity_decrease=0.0, min_impurity_split=None,\n",
    "                       min_samples_leaf=1, min_samples_split=2,\n",
    "                       min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,\n",
    "                       oob_score=False, random_state=0, verbose=0, warm_start=False)\n",
    "\n",
    "print(clf.feature_importances_)\n",
    "\n",
    "print(classification_report(y_test, clf.predict(\n",
    "    X_test), target_names=data[\"target_names\"]))\n",
    "\n",
    "joblib.dump(clf, 'classification.pkl')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Connect to clipper and register App"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "18-10-02:10:43:08 INFO     [kubernetes_container_manager.py:317] [default-cluster] Found 6 nodes: ec2-54-68-64-106.us-west-2.compute.amazonaws.com, ec2-34-219-191-153.us-west-2.compute.amazonaws.com, ec2-18-236-197-218.us-west-2.compute.amazonaws.com, ec2-54-212-98-152.us-west-2.compute.amazonaws.com, ec2-18-236-106-32.us-west-2.compute.amazonaws.com, ec2-52-13-37-198.us-west-2.compute.amazonaws.com\n",
      "18-10-02:10:43:08 INFO     [kubernetes_container_manager.py:328] [default-cluster] Setting Clipper mgmt port to 30091\n",
      "18-10-02:10:43:08 INFO     [kubernetes_container_manager.py:338] [default-cluster] Setting Clipper query port to 32273\n",
      "18-10-02:10:43:08 INFO     [kubernetes_container_manager.py:361] [default-cluster] Setting Clipper metric port to 30164\n",
      "18-10-02:10:43:08 INFO     [clipper_admin.py:151] [default-cluster] Successfully connected to Clipper cluster at 127.0.0.1:8001/api/v1/namespaces/mdt/services/query-frontend-at-default-cluster:1337/proxy\n",
      "18-10-02:10:43:08 INFO     [clipper_admin.py:215] [default-cluster] Application sklearn-classification was successfully registered\n"
     ]
    }
   ],
   "source": [
    "from clipper_admin import ClipperConnection, KubernetesContainerManager\n",
    "from clipper_admin.deployers import python as python_deployer\n",
    "manager = KubernetesContainerManager(\n",
    "    kubernetes_proxy_addr=K8S_PROXY_ADDR, namespace=K8S_NAMESPACE)\n",
    "clipper_conn = ClipperConnection(manager)\n",
    "clipper_conn.connect()\n",
    "\n",
    "clipper_conn.register_application(\n",
    "   name = APP_NAME, input_type = 'doubles', default_output = '0', slo_micros = 100000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a prediction function and deploy it to clipper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "18-10-02:10:45:05 INFO     [deployer_utils.py:41] Saving function to /var/folders/nk/xbb077ys5nd_t4mpb2s4fwfc0000gn/T/tmprdclQ1clipper\n",
      "18-10-02:10:45:05 INFO     [deployer_utils.py:51] Serialized and supplied predict function\n",
      "18-10-02:10:45:05 INFO     [python.py:192] Python closure saved\n",
      "18-10-02:10:45:05 INFO     [python.py:198] Using Python 2 base image\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:467] [default-cluster] Building model Docker image with model data from /var/folders/nk/xbb077ys5nd_t4mpb2s4fwfc0000gn/T/tmprdclQ1clipper\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster] Step 1/3 : FROM clipper/python-closure-container:develop\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster]  ---> 76a098e0f6ee\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster] Step 2/3 : RUN apt-get -y install build-essential && pip install sklearn\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster]  ---> Using cache\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster]  ---> 9ea83d4035e3\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster] Step 3/3 : COPY /var/folders/nk/xbb077ys5nd_t4mpb2s4fwfc0000gn/T/tmprdclQ1clipper /model/\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster]  ---> a3974ec080f7\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster] Successfully built a3974ec080f7\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:472] [default-cluster] Successfully tagged 658391232643.dkr.ecr.us-west-2.amazonaws.com/default-cluster-clipper-sklearn-predict:1\n",
      "18-10-02:10:45:05 INFO     [clipper_admin.py:474] [default-cluster] Pushing model Docker image to 658391232643.dkr.ecr.us-west-2.amazonaws.com/default-cluster-clipper-sklearn-predict:1\n",
      "18-10-02:10:45:15 INFO     [clipper_admin.py:651] [default-cluster] Successfully registered model clipper-sklearn-predict:1\n",
      "18-10-02:10:45:15 INFO     [clipper_admin.py:569] [default-cluster] Done deploying model clipper-sklearn-predict:1.\n"
     ]
    }
   ],
   "source": [
    "from sklearn.externals import joblib\n",
    "clf = joblib.load(MODEL_FILE)\n",
    "\n",
    "def predict_wrapper(X):\n",
    "    print(\"inputs {}\".format(X))\n",
    "    try:\n",
    "        result = clf.predict(X)\n",
    "        print(\"result is {}\".format(result))\n",
    "        ret = [str(i) for i in result]\n",
    "        print(\"return is {}\".format(ret))\n",
    "        return ret\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        return [str(e)]\n",
    "\n",
    "python_deployer.deploy_python_closure(clipper_conn,\n",
    "                                      name=MODEL_NAME,\n",
    "                                      version=VERSION,\n",
    "                                      input_type=\"doubles\",\n",
    "                                      func=predict_wrapper,\n",
    "                                      registry=REPO_URL,\n",
    "                                      pkgs_to_install=['sklearn'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Link the model to application"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "18-10-02:10:45:35 INFO     [clipper_admin.py:277] [default-cluster] Model clipper-sklearn-predict is now linked to application sklearn-classification\n"
     ]
    }
   ],
   "source": [
    "clipper_conn.link_model_to_app(app_name=APP_NAME, model_name=MODEL_NAME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"input\": [5.9, 3.0, 5.1, 1.8]}\n",
      "{u'default': False, u'output': 2, u'query_id': 37}\n"
     ]
    }
   ],
   "source": [
    "import requests, json, numpy as np\n",
    "def predict():\n",
    "    headers = {\"Content-type\": \"application/json\"}\n",
    "    data=json.dumps({\"input\": [5.9, 3.0, 5.1, 1.8]})\n",
    "    print(data)\n",
    "    url = \"http://{}/api/v1/namespaces/{}/services/query-frontend-at-default-cluster:1337/proxy/{}/predict\".format(K8S_PROXY_ADDR,K8S_NAMESPACE,APP_NAME)\n",
    "    res = requests.post(url, headers=headers, data=data)\n",
    "    return res.json()\n",
    "\n",
    "print(predict())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
